package cn.yunhe.controller;

import cn.yunhe.entity.*;
import cn.yunhe.services.IStudentCollService;
import cn.yunhe.services.IStudentServices;

import cn.yunhe.services.IXsTreamServices;
import cn.yunhe.services.impl.XsTreamServices;
import cn.yunhe.util.ListToXml;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@RestController
@RequestMapping("/sj")
public class LStudentController {

    public  static int index = 0;
    Problem randomProblem = new Problem();
    ArrayList arrayList  = new ArrayList();

    @Resource
    private IStudentServices studentServices;

    @Resource
    private cn.yunhe.services.LIStudentServices LIStudentServices;

    @Resource
    private IStudentCollService studentCollService;




    //转换  选项格式用
    @Resource(name = "xsTreamServices")
    private IXsTreamServices xsTreamServices;


    @RequestMapping("/toSuiji")
    public ModelAndView tolx() {

        return new ModelAndView("/student/tosjlx");
    }


    /**
     * 查找 课程名称
     *
     * @return
     */
    @RequestMapping("/tosj")
    public ModelAndView lx(HttpServletRequest request, HttpSession session) {

//显示所有课程 根据课程随机题
        List<Course> courseList = LIStudentServices.getCourseList();
        request.setAttribute("courseList", courseList);

        return new ModelAndView("/student/suiji");
    }

/*中间页面*/
    @RequestMapping("/tosjAll")
    public ModelAndView zhensuiji(){
        ModelAndView mv = new ModelAndView("/student/suijiAll");

       // System.out.println("学生选择的课程id是"+course_id);
        return mv;
    }


// 根据 选择练习
    @RequestMapping("/exercise")
    public Problem exer(int course_id){

        List<Problem> problemList = LIStudentServices.findProblemsByCourseId(course_id);

        Random random = new Random();
        int randomMin = 0;
        int randomMax = problemList.size();
        int aId = random.nextInt(randomMax) % (randomMax - randomMin + 1) + randomMin;

        if(arrayList.contains(aId)){
            aId = random.nextInt(randomMax) % (randomMax - randomMin + 1) + randomMin;
        }

        arrayList.add(aId);

        if (index<problemList.size()){
            randomProblem = problemList.get(aId);
            index+=1;
        }else {
            System.out.println("题做完了");
            index = 0;
            randomProblem = problemList.get(aId);
            index+=1;
        }



        XsRequset xsRequset = xsTreamServices.xmlToObjext(randomProblem.getPro_choice(), XsRequset.class);

        LinkedHashMap<String, String> hashMap = xsRequset.getChoiceList();

        randomProblem.setPro_mapname(hashMap);

        System.out.println("控制层执行了");
        return randomProblem;
    }



    //收藏　　　　题
    @RequestMapping("/shoucang")
    public String insertColl(int pro_id,String coll_remark,HttpSession session){

        System.out.println("收藏备注是:"+coll_remark);
        int student_id = 1;//学生 id　


        HashMap hashMap = new HashMap();
        hashMap.put("pro_id",pro_id);
        hashMap.put("student_id",student_id);
        hashMap.put("coll_remark",coll_remark);


        //判断=是否 有藏过该题
        Boolean isColl = studentCollService.judgeColl(hashMap);
        if(isColl){
            System.out.println("已经添加过该题了");
            return "0";
        }else {
            studentCollService.insertPro(hashMap);
            return "1";
        }
    }

    //跳转页面
    @RequestMapping(value = "/toHis")
    public ModelAndView toHis(){
        return new ModelAndView("/student/historyExam");
    }

// 返回数据
    @RequestMapping(value = "/history")
    public Map historyExam(){
        int score_id = 7;
        Score score = LIStudentServices.findHistoryPaperById(score_id);

        ListToXml util = new ListToXml();
        //转换 答案  答案+题目List
        List<StudentProblem> answerAndProblem = util.xmlToList(score.getAnswer());

        System.out.println("历史考题总长度为："+answerAndProblem.size());

        List danxuanList = new ArrayList();
        List duoxuanList = new ArrayList();
        List panduanList = new ArrayList();
        Map map = new HashMap();
        for (int i = 0;i<answerAndProblem.size();i++){
            String type = answerAndProblem.get(i).getType();

            switch (type){
                case "单选题": {
                    danxuanList.add(answerAndProblem.get(i));
                    break;
                }
                case "多选题":{
                   duoxuanList.add(answerAndProblem.get(i));
                    break;
                }
                case "判断题":{
                    panduanList.add(answerAndProblem.get(i));
                    break;
                }
            }
        }


        map.put("only",danxuanList);
        map.put("many",duoxuanList);
        map.put("pd",panduanList);
        System.out.println(danxuanList.get(0).toString());
        System.out.println("该Map返回前台，题类型有"+map.size()+"个");
        System.out.println("单选类型长度有"+danxuanList.size());
        System.out.println("多选类型长度有"+duoxuanList.size());
        System.out.println("判断类型长度有"+panduanList.size());



        return map;
    }




    public cn.yunhe.services.LIStudentServices getLIStudentServices() {
        return LIStudentServices;
    }

    public void setLIStudentServices(cn.yunhe.services.LIStudentServices LIStudentServices) {
        this.LIStudentServices = LIStudentServices;
    }

    public IStudentServices getStudentServices() {
        return studentServices;
    }

    public void setStudentServices(IStudentServices studentServices) {
        this.studentServices = studentServices;
    }
}
